Functions¶

def¶

You can define more than one function in a script.

three_reds.py¶

snapshot¶

Bit has the ability to take a snapshot of the world using the snapshot function.

snap_three_reds.py¶

You can use any phrase you want when making a snapshot.

The Jump buttons take you to the previous or next snapshot.

Naming variables¶

cosmo.py¶

When naming a function or variable:

  • Use underscore _ (next to the zero key + shift) to break up compound names
    • go_green instead of gogreen or goGreen
  • use lower-case characters
    • go instead of Go or GO
    • Casing matters: go is different from Go!

In blue_squares.py, when will Bit paint a blue square?

blue_squares.py¶

In [5]:
%%file for_class/blue_squares.py
from byubit import Bit

def reds(bit):
    bit.paint('red')
    bit.move()
    bit.paint('red')
    bit.move()
    
def blues(bit):
    bit.paint('blue')
    bit.move()
    bit.paint('blue')
    bit.move()
    
def greens(bit):
    bit.paint('green')
    bit.move()
    bit.paint('green')
    bit.move()

@Bit.empty_world(5, 3)
def run(bit):
    reds(bit)
    greens(bit)
    
if __name__ == '__main__':
    run(Bit.new_bit)
Overwriting for_class/blue_squares.py

Defining a function is not the same as calling a function.

Docstrings¶

The very first line after def can have a string. When this is the case, we call that string a docstring because it documents the function: you use it to describe what the function is for and how to use it.

Strings that use single quotation characters (e.g. " or ') are only one line.

If you want to use multiple lines for your string, use triple quotes (""" or ''').

NOTES

Gordon Bean:

When our oldest child was just learning how to crawl, my wife and I discovered that our daughter would often get right up behind us without us realizing it. When we would turn around to move to another activity, we would stumble over her. It wasn't a great setup. We were often warning each other that there was a baby right behind. So one day my wife declared: "We need a code word we can use to say 'be careful turning around: there is a baby right behind you'" I agreed that was a good idea, and after a moment's thought she declared "jellyfish".

So, for several years, we've used the term jellyfish to communicate "watch out, there is someone/something right behind you that you might run into as you turn around". It's become second nature. So much, in fact, that I almost used it with a colleague one day as I navigated a crowded room and was passing right behind her.

Human beings have a natural ability for creating ideas ("watch out for the baby right behind you") and giving those ideas names ("jellyfish").

Defining functions is just like that. We get to come up with the idea we want and name it. Then we can use it just like it is an ordinary word.

Functions give you the ability to create and name new ideas.

Smiles 😁 😃 🙂 🙃¶

Given the following starting world:

Draw four blue smiles:

smiles.py¶

  • Draw out a strategy
  • Identify the pieces
    • What are the ideas, how do they fit together?
  • Implement one piece at a time

Key Ideas¶

  • Define functions with def
  • Make functions to represent complex ideas: abstraction
  • Make functions to define tasks that will be repeated
  • Naming functions and variables
  • Docstrings